﻿using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Commons;
using System;
using System.Collections.Generic;
namespace eTMS.Controls
{
    public abstract class ComboBoxBindingBase<T> : ComboBox
    {

        public ComboBoxBindingBase()
        {
            //BindData();
        }
        private bool _IsAddChooseItem = false;
        private bool _IsAddAllItem = false;
        public bool IsAddChooseItem
        {
            set
            {
                _IsAddChooseItem = value;
                if (DesignMode) return;
                if (value)
                {
                    BindData();
                }
            }
            get
            {
                return _IsAddChooseItem;
            }
        }
        public bool IsAddAllItem
        {
            set
            {
                _IsAddAllItem = value;
                if (DesignMode) return;
                if (value)
                {
                    BindData();
                }
            }
            get
            {
                return _IsAddAllItem;
            }
        }
        protected bool IsBinded = false;

        Dictionary<string, object> searchValues = new Dictionary<string, object>();
        public void AddSearchValues(string FieldName, object value)
        {
            if (!searchValues.ContainsKey(FieldName))
            {
                searchValues.Add(FieldName, value);
            }
            else
            {
                searchValues[FieldName] = value;
            }
        }
        public void ClearSearchValue()
        {
            searchValues.Clear();
        }
        protected List<T> FilterList(List<T> ls)
        {
            if (searchValues.Count == 0) return ls;
            if (ls.Count == 0) return ls;
            System.Reflection.PropertyInfo[] fields = ls[0].GetType().GetProperties();
            return ls.FindAll(delegate(T oFind)
            {
                bool found = true;
                foreach (string key in searchValues.Keys)
                {
                    foreach (System.Reflection.PropertyInfo f in fields)
                    {
                        if (key.ToLower() == f.Name.ToLower())
                        {
                            if (!f.GetValue(oFind, null).ToString().Equals(searchValues[key].ToString()))
                                found =false;
                        }
                    }
                }
                return found;
            }
            );
        }
        public abstract void BindData();

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.F5)
            {
                BindData();
            }
        }
    }
}
